package com.wpzf.ndhc.utils;

import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import org.gdal.osr.SpatialReference;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class WpzfNdhcGdalUtils {
    public String exportTbList(HttpServletResponse response, List<Map<String, Object>> dataList, String tempath) {
        ogr.RegisterAll();
        String rest = "";
        try {
            String nowDate = new SimpleDateFormat("yyyyMMddHHMMss").format(new Date());
            String shapefilePath = tempath + "/图斑列表" + nowDate + ".shp";
//        gdal.SetConfigOption("GDAL_DATA","D:\\project\\周报\\GDAL_Release");

            gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
            gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");
            gdal.SetConfigOption("SHAPE_ENCODING", "CP936");

            Driver driver = ogr.GetDriverByName("ESRI Shapefile");
            if (driver == null) {
                System.out.println("打开驱动器失败");
                return "error,打开驱动器失败";
            }


            /*获取核查结果*/
            DataSource dataSource = driver.CreateDataSource(shapefilePath);
            if (dataSource == null) {
                System.out.println("创建数据源失败");
                return "error,创建数据源失败";
            }
            // 定义坐标系
            SpatialReference spatialReference = new SpatialReference("");
            spatialReference.SetWellKnownGeogCS("WGS84");
//            SpatialReference.ImportFromEPSG(4490);

            // 创建一个新的图层
            Layer layer = dataSource.CreateLayer("new_layer", spatialReference, ogr.wkbPolygon, null);

            // 添加新字段 smc
            FieldDefn tbbsm = new FieldDefn("tbbsm", ogr.OFTString);
            tbbsm.SetWidth(254);
            layer.CreateField(tbbsm);

            FieldDefn xzqdm = new FieldDefn("xzqdm", ogr.OFTString);
            xzqdm.SetWidth(254);
            layer.CreateField(xzqdm);

            FieldDefn xzqmc = new FieldDefn("xzqmc", ogr.OFTString);
            xzqmc.SetWidth(254);
            layer.CreateField(xzqmc);

            FieldDefn xzdm = new FieldDefn("xzdm", ogr.OFTString);
            xzdm.SetWidth(254);
            layer.CreateField(xzdm);

            FieldDefn xzmc = new FieldDefn("xzmc", ogr.OFTString);
            xzmc.SetWidth(254);
            layer.CreateField(xzmc);

            FieldDefn jcbh = new FieldDefn("jcbh", ogr.OFTString);
            jcbh.SetWidth(254);
            layer.CreateField(jcbh);

            FieldDefn tbtz = new FieldDefn("tbtz", ogr.OFTString);
            tbtz.SetWidth(254);
            layer.CreateField(tbtz);

            FieldDefn jcmj = new FieldDefn("jcmj", ogr.OFTReal);
            jcmj.SetWidth(254);
            layer.CreateField(jcmj);

            FieldDefn jctqmj = new FieldDefn("jctqmj", ogr.OFTReal);
            jctqmj.SetWidth(254);
            layer.CreateField(jctqmj);


            for (Map<String, Object> item : dataList) {
                //创建一个新的几何对象，这里是点
                Feature feature = new Feature(layer.GetLayerDefn());
                if (item.get("shape") != "") {
//                    System.out.println(item.getWkt().split(";")[0].toString());
                    feature.SetGeometry(ogr.CreateGeometryFromWkt(item.get("shape") + "".split(";")[0].toString()));
                }
                feature.SetField("tbbsm", item.get("tbbsm") + "");
                feature.SetField("xzqdm", item.get("xzqdm") + "");
                feature.SetField("xzqmc", item.get("xzqmc") + "");
                feature.SetField("xzdm", item.get("xzdm") + "");
                feature.SetField("xzmc", item.get("xzmc") + "");
                feature.SetField("jcbh", item.get("jcbh") + "");
                feature.SetField("tbtz", item.get("tbtz") + "");
                feature.SetField("jcmj", item.get("jcmj") + "");
                feature.SetField("jctqmj", item.get("jctqmj") + "");

                //添加特征到图层
                layer.CreateFeature(feature);
            }
            layer.SyncToDisk();
            rest = "图斑列表" + nowDate + ".shp";
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            rest = "error";
        }
        return rest;
    }


    public String exportDkList(HttpServletResponse response, List<Map<String,Object>> dataList, String tempath) {
        ogr.RegisterAll();
        String rest = "";
        try {
            String nowDate = new SimpleDateFormat("yyyyMMddHHMMss").format(new Date());
            String shapefilePath = tempath + "/地块列表" + nowDate + ".shp";

            gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
            gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");
            gdal.SetConfigOption("SHAPE_ENCODING", "CP936");

            Driver driver = ogr.GetDriverByName("ESRI Shapefile");
            if (driver == null) {
                System.out.println("打开驱动器失败");
                return "error,打开驱动器失败";
            }


            //有要素
            /*获取核查结果*/
            DataSource dataSource = driver.CreateDataSource(shapefilePath);
            if (dataSource == null) {
                System.out.println("创建数据源失败");
                return "error,创建数据源失败";
            }
            // 定义坐标系
            SpatialReference spatialReference = new SpatialReference("");
            spatialReference.SetWellKnownGeogCS("WGS84");

            // 创建一个新的图层
            Layer layer = dataSource.CreateLayer("new_layer", spatialReference, ogr.wkbPolygon, null);

            // 添加新字段 smc
            FieldDefn bsm = new FieldDefn("bsm", ogr.OFTString);
            bsm.SetWidth(254);
            layer.CreateField(bsm);

            FieldDefn tbbsm = new FieldDefn("tbbsm", ogr.OFTString);
            tbbsm.SetWidth(254);
            layer.CreateField(tbbsm);

            FieldDefn xzqdm = new FieldDefn("xzqdm", ogr.OFTString);
            xzqdm.SetWidth(254);
            layer.CreateField(xzqdm);

            FieldDefn xmc = new FieldDefn("xmc", ogr.OFTString);
            xmc.SetWidth(254);
            layer.CreateField(xmc);

            FieldDefn dkbh = new FieldDefn("dkbh", ogr.OFTString);
            dkbh.SetWidth(254);
            layer.CreateField(dkbh);

            FieldDefn dkmc = new FieldDefn("dkmc", ogr.OFTString);
            dkmc.SetWidth(254);
            layer.CreateField(dkmc);

            FieldDefn dkmj = new FieldDefn("dkmj", ogr.OFTString);
            dkmj.SetWidth(254);
            layer.CreateField(dkmj);

            FieldDefn xzb = new FieldDefn("xzb", ogr.OFTString);
            xzb.SetWidth(254);
            layer.CreateField(xzb);

            FieldDefn yzb = new FieldDefn("yzb", ogr.OFTString);
            yzb.SetWidth(254);
            layer.CreateField(yzb);

            FieldDefn bz = new FieldDefn("bz", ogr.OFTString);
            bz.SetWidth(254);
            layer.CreateField(bz);

            FieldDefn dkfw = new FieldDefn("dkfw", ogr.OFTString);
            dkfw.SetWidth(254);
            layer.CreateField(dkfw);

            FieldDefn kzxx = new FieldDefn("kzxx", ogr.OFTString);
            kzxx.SetWidth(254);
            layer.CreateField(kzxx);

            FieldDefn sfbgjsyd = new FieldDefn("sfbgjsyd", ogr.OFTString);
            sfbgjsyd.SetWidth(254);
            layer.CreateField(sfbgjsyd);

            FieldDefn sfgdwf = new FieldDefn("sfgdwf", ogr.OFTReal);
            sfgdwf.SetWidth(254);
            layer.CreateField(sfgdwf);

            FieldDefn gdmj = new FieldDefn("gdmj", ogr.OFTString);
            gdmj.SetWidth(254);
            layer.CreateField(gdmj);

            FieldDefn qtnydmj = new FieldDefn("qtnydmj", ogr.OFTString);
            qtnydmj.SetWidth(254);
            layer.CreateField(qtnydmj);

            FieldDefn jsydmj = new FieldDefn("jsydmj", ogr.OFTString);
            jsydmj.SetWidth(254);
            layer.CreateField(jsydmj);

            FieldDefn wlydmj = new FieldDefn("wlydmj", ogr.OFTString);
            wlydmj.SetWidth(254);
            layer.CreateField(wlydmj);


            for (Map<String,Object> item : dataList) {
                //创建一个新的几何对象，这里是点
                Feature feature = new Feature(layer.GetLayerDefn());
                if (item.get("dkfw") + "" != "") {
//                    System.out.println(item.getWkt().split(";")[0].toString());
                    System.out.println(item.get("dkfw").toString());
                    try {
                        feature.SetGeometry(ogr.CreateGeometryFromWkt(item.get("dkfw").toString()));
                        //feature.SetGeometry(ogr.CreateGeometryFromWkt(item.get("shape") + "".split(";")[0].toString()));
                    }
                    catch(Exception exception)
                    {

                    }
                  }
                feature.SetField("bsm", item.get("bsm")+"");
                feature.SetField("tbbsm", item.get("tbbsm")+"");
                feature.SetField("xzqdm", item.get("xzqdm")+"");
                feature.SetField("xmc", item.get("xmc")+"");
                feature.SetField("dkbh", item.get("dkbh")+"");
                feature.SetField("dkmc", item.get("dkmc")+"");
                feature.SetField("dkmj", item.get("dkmj")+"");
                feature.SetField("xzb", item.get("xzb")+"");
                feature.SetField("yzb", item.get("yzb")+"");
                feature.SetField("bz", item.get("bz")+"");
                //feature.SetField("dkfw", item.get("dkfw")+"");
                feature.SetField("sfgdwf", item.get("sfgdwf")+"");
                feature.SetField("gdmj", item.get("gdmj")+"");
                feature.SetField("qtnydmj", item.get("qtnydmj")+"");
                feature.SetField("jsydmj", item.get("jsydmj")+"");
                feature.SetField("wlydmj", item.get("wlydmj")+"");
                //添加特征到图层
                layer.CreateFeature(feature);
            }
            layer.SyncToDisk();
            rest = "地块列表" + nowDate + ".shp";
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            rest = "error";
        }
        return rest;
    }

}
